import 'package:flutter_test/flutter_test.dart';
import 'package:demo/models/counter_model.dart';

void main() {
  group('CounterModel Tests', () {
    late CounterModel counterModel;

    setUp(() {
      counterModel = CounterModel();
    });

    test('初始状态应该正确', () {
      expect(counterModel.counter, 0);
      expect(counterModel.isLoading, false);
      expect(counterModel.username, 'Guest');
      expect(counterModel.counterStatus, 'Zero');
    });

    test('increment() 应该增加计数器', () {
      counterModel.increment();
      expect(counterModel.counter, 1);

      counterModel.increment();
      expect(counterModel.counter, 2);
    });

    test('decrement() 应该减少计数器', () {
      // 先增加一些值
      counterModel.addMultiple(5);
      expect(counterModel.counter, 5);

      counterModel.decrement();
      expect(counterModel.counter, 4);

      counterModel.decrement();
      expect(counterModel.counter, 3);
    });

    test('decrement() 不应该让计数器变为负数', () {
      expect(counterModel.counter, 0);

      counterModel.decrement();
      expect(counterModel.counter, 0); // 应该保持为0
    });

    test('reset() 应该重置计数器为0', () {
      counterModel.addMultiple(10);
      expect(counterModel.counter, 10);

      counterModel.reset();
      expect(counterModel.counter, 0);
    });

    test('setUsername() 应该更新用户名', () {
      counterModel.setUsername('Alice');
      expect(counterModel.username, 'Alice');

      counterModel.setUsername('Bob');
      expect(counterModel.username, 'Bob');
    });

    test('addMultiple() 应该批量增加计数器', () {
      counterModel.addMultiple(5);
      expect(counterModel.counter, 5);

      counterModel.addMultiple(10);
      expect(counterModel.counter, 15);
    });

    test('counterStatus 应该根据计数器值返回正确状态', () {
      expect(counterModel.counterStatus, 'Zero');

      counterModel.addMultiple(5);
      expect(counterModel.counterStatus, 'Low');

      counterModel.addMultiple(10); // 总共15
      expect(counterModel.counterStatus, 'Medium');

      counterModel.addMultiple(40); // 总共55
      expect(counterModel.counterStatus, 'High');
    });

    test('incrementAsync() 应该异步增加计数器', () async {
      expect(counterModel.isLoading, false);
      expect(counterModel.counter, 0);

      // 开始异步操作
      final future = counterModel.incrementAsync();

      // 应该立即设置为加载状态
      expect(counterModel.isLoading, true);
      expect(counterModel.counter, 0); // 计数器还没有增加

      // 等待异步操作完成
      await future;

      // 操作完成后
      expect(counterModel.isLoading, false);
      expect(counterModel.counter, 1);
    });

    test('状态改变应该正确更新', () {
      // 测试基本状态改变
      expect(counterModel.counter, 0);

      counterModel.increment();
      expect(counterModel.counter, 1);

      counterModel.setUsername('Test');
      expect(counterModel.username, 'Test');

      counterModel.reset();
      expect(counterModel.counter, 0);
      expect(counterModel.username, 'Test'); // 用户名应该保持
    });

    test('多个操作的组合测试', () {
      // 设置用户名
      counterModel.setUsername('TestUser');
      expect(counterModel.username, 'TestUser');

      // 增加计数器
      counterModel.addMultiple(25);
      expect(counterModel.counter, 25);
      expect(counterModel.counterStatus, 'Medium');

      // 继续增加
      counterModel.increment();
      counterModel.increment();
      expect(counterModel.counter, 27);

      // 减少
      counterModel.decrement();
      expect(counterModel.counter, 26);

      // 重置
      counterModel.reset();
      expect(counterModel.counter, 0);
      expect(counterModel.counterStatus, 'Zero');
      expect(counterModel.username, 'TestUser'); // 用户名应该保持不变
    });
  });
}
